package leetcode.offer;

import entity.ListNode;
import org.junit.Test;

import java.util.List;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2021/11/18
 */
public class offer18 {


    @Test
    public void test() {
        ListNode listNode = new ListNode(4, new ListNode(5, new ListNode(1, new ListNode(4, new ListNode(9)))));
        deleteNode(listNode, 1);
        System.out.println(listNode);
    }


    public ListNode deleteNode(ListNode head, int val) {
        if (head.val == val) {
            return head.next;
        }
        // 当前节点
        ListNode p = head;
        // 当前节点的下个节点
        ListNode q = head.next;
        while (q != null && q.val != val) {
            p = q;
            q = p.next;
        }
        if (q != null) {
            p.next = q.next;
        }
        return head;
    }
}
